package com.quasar.hpatient.module.comm_main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.quasar.hpatient.R;
import com.quasar.hpatient.module.doctor_chat.DoctorChatActivity;

import java.io.Serializable;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.chat.ChatModel;
import lib.quasar.chat.ChatService;
import lib.quasar.context.BaseApp;
import lib.quasar.db.table.Chat;
import lib.quasar.pedometer.PedometerManager;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.menu.TabMenuLayout;

/**
 * description: 入口
 * created by kalu on 2018/4/2 16:01
 */
public class MainActivity extends BaseActivity<MainPresenter> implements MainView {

    public static final String H5_URL = "url";
    public final static String LOGOUT = "LOGOUT";
    private long currTime = Activity.DEFAULT_KEYS_DISABLE;

    @Override
    public int initView() {
        return R.layout.activity_comm_main;
    }

    @Override
    public void initDataNet() {

        getPresenter().loadPatientInfo(this);

        // 计步器服务
        PedometerManager.init(getApplicationContext());

        final String stringExtra = getIntent().getStringExtra(H5_URL);
        if (!TextUtils.isEmpty(stringExtra)) {
            Toast.makeText(getApplicationContext(), stringExtra, Toast.LENGTH_SHORT).show();
        }

        getPresenter().initAdapter(this, getSupportFragmentManager());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPresenter().startThread();
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPresenter().unbindChatService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getPresenter().stopThread();
    }

    @Override
    protected void onResume() {
        getPresenter().refreshMessage(this);
        getPresenter().startChatService(this);
        getPresenter().bindChatService(this);
        super.onResume();
    }

    @Override
    public void onBackPressed() {

        long secondTime = System.currentTimeMillis();
        if (secondTime - currTime > 2000) {
            currTime = secondTime;
            toast("再次点击退出应用");
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void logout() {
        currTime = System.currentTimeMillis();
        onBackPressed();
    }

    @Override
    public boolean isHome() {
        ViewPager pager = findViewById(R.id.main_viewpager);


        return pager.getCurrentItem() == 0;
    }

    @Override
    public void setPagerAdapter(PagerAdapter adapter) {
        ViewPager pager = findViewById(R.id.main_viewpager);
        if (null == pager) return;
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(adapter);
    }

    @Override
    public void setMenuPager(int messageNumber) {
        TabMenuLayout menu = findViewById(R.id.main_tabmenu);
        if (null == menu) return;
        ViewPager pager = findViewById(R.id.main_viewpager);
        if (null == pager) return;
        menu.setViewPager(pager);
        menu.setBadgeMessage(2, messageNumber);
        menu.setBadgeMessage(3);
        menu.setOnTabMenuChangedListener((isSwitch, isClick, isClickSelected, position) -> {

        });
    }

    @Override
    public void initDataLocal() {

        if (null == getIntent())
            return;

        Serializable serializableExtra = getIntent().getSerializableExtra(ChatService.MESSAGE_MODEL);
        if (null != serializableExtra) {
            final Chat chat = (Chat) serializableExtra;
            if (null != chat) {
                LogUtil.e("MainActivity", "initDataLocal ==> " + chat.toString());
                // 化验单标识符
                if (Chat.INSPECTION == chat.getMessage_type()) {
                    String inspectionJson = chat.getJson_message_content();
                    ChatModel.InspectionModel inspectionModel = new Gson().fromJson(inspectionJson, ChatModel.InspectionModel.class);
                    if (null != inspectionModel) {
                        toast("跳转化验单");
                    }
                } else {
                    //聊天界面
                    Intent intent = new Intent(BaseApp.getActivity(), DoctorChatActivity.class);
                    intent.putExtra(DoctorChatActivity.CHAT_ID, String.valueOf(chat.getSendid()));
                    intent.putExtra(DoctorChatActivity.CHAT_NAME, String.valueOf(chat.getSend_name()));
                    goNext(intent);
                }
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (null == getIntent())
            return;

        Serializable serializableExtra = getIntent().getSerializableExtra(ChatService.MESSAGE_MODEL);
        if (null != serializableExtra) {
            final Chat chat = (Chat) serializableExtra;
            if (null != chat) {
                LogUtil.e("MainActivity", "initDataLocal ==> " + chat.toString());
                if (Chat.INSPECTION == chat.getMessage_type()) {
                    String inspectionJson = chat.getJson_message_content();
                    ChatModel.InspectionModel inspectionModel = new Gson().fromJson(inspectionJson, ChatModel.InspectionModel.class);
                    if (null != inspectionModel) {
                        toast("跳转化验单");
                    }
                } else {
                    Intent intent2 = new Intent(BaseApp.getActivity(), DoctorChatActivity.class);
                    intent2.putExtra(DoctorChatActivity.CHAT_ID, String.valueOf(chat.getSendid()));
                    intent2.putExtra(DoctorChatActivity.CHAT_NAME, String.valueOf(chat.getSend_name()));

                    goNext(intent2);
                }
            }
        }
    }

    @Override
    public void autoBadgeNumber() {
        TabMenuLayout menu = findViewById(R.id.main_tabmenu);
        if (null == menu) return;
        menu.autoIncrementMessage(0);
    }

    @Override
    public void setBadgeNumber(int badgeNumber) {
        TabMenuLayout menu = findViewById(R.id.main_tabmenu);
        if (null == menu) return;
        menu.setBadgeMessage(0, badgeNumber);
    }

    @Override
    public void minusBadgeNumber(int badgeNumber) {
        TabMenuLayout menu = findViewById(R.id.main_tabmenu);
        if (null == menu) return;
        menu.minusBadgeNumber(0, badgeNumber);
    }
}